/**
 *
 */
package com.angel.data.generator.validators.impl;

import java.lang.reflect.Method;

import com.angel.common.helpers.AnnotationHelper;
import com.angel.common.helpers.ReflectionHelper;
import com.angel.data.generator.annotations.importFileProcessorRunner.ImportFileProcessorRunnerBuilder;
import com.angel.data.generator.exceptions.DataGeneratorBuilderValidatorException;
import com.angel.data.generator.validators.DataGeneratorBuilderValidator;
import com.angel.io.descriptor.FileProcessorDescriptor;

/**
 * @author William
 *
 */
public class ImporFileProcessorRunnerMethodValidator implements DataGeneratorBuilderValidator{

	public void validate(Class<?> dataGeneratorClass) throws DataGeneratorBuilderValidatorException {
		Method[] methods = ReflectionHelper.getAllMethodFor(dataGeneratorClass);
		boolean hasOnlyOne = AnnotationHelper.hasOnlyOneAnnotationInstance(methods, ImportFileProcessorRunnerBuilder.class);
		if(hasOnlyOne) {
			Method importFileProcessorRunnerMethod = AnnotationHelper.getMethodWithAnnotation(dataGeneratorClass, ImportFileProcessorRunnerBuilder.class);
			Class<?>[] parameters = importFileProcessorRunnerMethod.getParameterTypes();
			if(parameters != null && parameters.length == 1){
				boolean inherit = FileProcessorDescriptor.class.equals(parameters[0]) || ReflectionHelper.inheritClassFrom(parameters[0], FileProcessorDescriptor.class);
				if(!inherit) {
					throw new DataGeneratorBuilderValidatorException("Method with ImporFileProcessorRunnerBuilder annotation must have parameter object from class FileProcessorDescriptor or a subclass.");
				}
			} else {
				throw new DataGeneratorBuilderValidatorException("Method with ImporFileProcessorRunnerBuilder annotation must have one and only one parameter object from class FileProcessorDescriptor or a subclass.");
			}
		} else {
			throw new DataGeneratorBuilderValidatorException("Data object generator class [" + dataGeneratorClass + "] contains [0 or more than 1] ImporFileProcessorRunnerBuilder annotation.");
		}
	}
}
